#!/bin/bash
# ***************************************************************
# *                    simula.plus@cemes.fr                     *
# *	              GNU/linux version 3.2.0                   *
# *            software under General Public License            *
# ***************************************************************
# * copyright © 2003,2004,2005,2006,2007,2009,2010,2012 COLLARD Christophe
# * copyright © 2003,2004,2005,2006,2007,2009,2010,2012 Centre National de la Recherche Scientifique
# * copyright © 2003,2004,2005,2006,2007,2009,2010 Arts et Métiers ParisTech
# * copyright © 2003,2004,2005,2006,2007 Université de Valenciennes et du Hainaut-Cambrésis
# * copyright © 2003,2004,2005,2006,2007,2009,2010 Laboratoire de Physique et Mécanique des Matériaux
# * copyright © 2003,2004,2005,2006,2007 Laboratoire de Mathématiques et ses Applications de Valenciennes
# * copyright © 2010,2012 Centre d'Elaboration de Matériaux et d'Etudes Structurales (CEMES - CNRS)
# ***************************************************************/
#    run simula+ tests belongs to Simula+ </br>
#
#    Simula+ is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version. <br><br>
#
#    Simula+ is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details. <br><br>
#
#    You should have received a copy of the GNU General Public License
#    along with Simula+; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


# configure tests to execute
version="3.2"
run_maths="no"
run_mol="yes"
run_femol="no"
run_materiol="no"
run_materials="no"
run_stmol="no"
run_pmol="no"
run_mpi_pmol="no"
run_heterogeneities="no"
run_sma="no"
run_sma_pheno="no"
run_3_phases_model="no"
run_mpisol="no"


# color definition
GREEN="\\033[1;32m"
NORMAL="\\033[0;39m"
RED="\\033[1;31m"
PINK="\\033[1;35m"
BLUE="\\033[1;34m"
WHITE="\\033[0;02m"
LIGHTWHITE="\\033[1;08m"
YELLOW="\\033[1;33m"
CYAN="\\033[1;36m"

reset
echo -e $GREEN"Simula+" $version "(C) 2002-2012" $NORMAL
echo "Simula+ is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Simula+ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with Simula+; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA"

# search for simula+ directory
if [ "$SimulaPlus" = "" ]; then
    cd ..
    SimulaPlus="$(pwd)"
    if [ -d $SimulaPlus/MOL++ ]; then
	echo -e $GREEN"Simula+ found in "$SimulaPlus$NORMAL
    else
	echo -e $RED"Simula+ not found"
	echo -e "Please define simula+ path in SimulaPlus variable or run program from the simula+/tests directory"$NORMAL
	exit
    fi
else
    if [ -d $SimulaPlus/MOL++ ]; then
	echo -e $GREEN"Simula+ found in "$SimulaPlus$NORMAL
    else
	echo -e $RED"Simula+ not found"
	echo -e "Wrong path defined for simula+"$NORMAL
	exit
    fi
fi
export SimulaPlus
export rat=true
cd $SimulaPlus/tests


# look for a queue manager
queue_manager=$(ps -edf | grep pbs_server | grep -v grep | wc -l)
if [ "$queue_manager" == "0" ]; then
    echo -e $BLUE"warning : no queue manager found" $NORMAL
else
    echo -e $GREEN"pbs queue manager found" $NORMAL
fi    
if [ ! -d log ]; then
    echo -e $GREEN"creating log directory" $NORMAL
    mkdir log
fi


# look for a compiler
compiler=$(which icc |grep icc)
if [ "$compiler" = "" ]; then
   compiler=$(which g++ |grep g++)
   if [ "$compiler" = "" ]; then
      echo -e $RED"fatal error : no compiler found" $NORMAL
      exit
   else echo -e $GREEN"using g++ compiler" $NORMAL
   fi
else echo -e $GREEN"using intel C++ compiler" $NORMAL
fi
echo -e $GREEN$($compiler --version) $NORMAL
export compiler


# execution of maths
if [ -f run\ maths -a "$run_maths" = "yes" ]; then
    if [ -f pbs/maths.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting maths : " $NORMAL
	qsub pbs/maths.pbs&
    else
	sh run\ maths
    fi
fi


# execution of MOL++
if [ -f run\ MOL++ -a "$run_mol" = "yes" ]; then
    if [ -f pbs/mol.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting MOL++ : " $NORMAL
	qsub pbs/mol.pbs& 
    else
	sh run\ MOL++
    fi
fi


# execution of FEMOL++
if [ -f run\ FEMOL++ -a "$run_femol" = "yes" ]; then
    if [ -f pbs/femol.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting FEMOL++ : " $NORMAL
	qsub pbs/femol.pbs& 
    else
	sh run\ FEMOL++
    fi
fi


# execution of MateriOL++
if [ -f run\ MateriOL++ -a "$run_materiol" = "yes" ]; then
    if [ -f pbs/materiol.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting MateriOL++ : " $NORMAL
	qsub pbs/materiol.pbs& 
    else
	sh run\ MateriOL++
    fi
fi


# execution of materials
if [ -f run\ materials -a "$run_materials" = "yes" ]; then
    if [ -f pbs/materials.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting materials : " $NORMAL
	qsub pbs/materials.pbs& 
    else
	sh run\ materials
    fi
fi


# execution of STMOL++
if [ -f run\ STMOL++ -a "$run_stmol" = "yes" ]; then
    if [ -f pbs/stmol.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting STMOL++ : " $NORMAL
	qsub pbs/stmol.pbs& 
    else
	sh run\ STMOL++
    fi
fi


# execution of PMOL++
if [ -f run\ PMOL++ -a "$run_pmol" = "yes" ]; then
    if [ -f pbs/pmol.pbs -a "$queue_manager" == "1" ]; then
        echo -en $CYAN"submitting PMOL++ : " $NORMAL
        qsub pbs/pmol.pbs&
    else
        sh run\ PMOL++
    fi
fi


# execution of MPI PMOL++
if [ -f run\ MPI\ PMOL++ -a "$run_mpi_pmol" = "yes" ]; then
    if [ -f pbs/mpi.pmol.pbs -a "$queue_manager" == "1" ]; then
        echo -en $CYAN"submitting PMOL++ : " $NORMAL
        qsub pbs/mpi.pmol.pbs&
    else
        sh run\ MPI\ PMOL++ no
    fi
fi


# execution of heterogeneities
if [ -f run\ heterogeneities -a "$run_heterogeneities" = "yes" ]; then
    if [ -f pbs/heterogeneities.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting heterogeneities : " $NORMAL
	qsub pbs/heterogeneities.pbs& 
    else
	sh run\ heterogeneities
    fi
fi


# execution of sma
if [ -f run\ sma -a "$run_sma" = "yes" ]; then
    if [ -f pbs/sma.pbs -a "$queue_manager" = "1" ]; then
	echo -en $CYAN"submitting sma : " $NORMAL
	qsub pbs/sma.pbs& 
    else
	sh run\ sma
    fi
fi


# execution of sma_pheno
if [ -f run\ sma -a "$run_sma_pheno" = "yes" ]; then
    if [ -f pbs/sma-pheno.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting phenomenological sma model : " $NORMAL
	qsub pbs/sma-pheno.pbs& 
    else
	sh run\ sma\ pheno
    fi
fi


# execution of 3_phases_model
if [ -f run\ 3\ phases\ model -a "$run_3_phases_model" = "yes" ]; then
    if [ -f pbs/3_phases_model.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting sma : " $NORMAL
	qsub pbs/3_phases_model.pbs& 
    else
	sh run\ 3\ phases\ model
    fi
fi


# execution of MPISOL++
if [ -f run\ MPISOL++ -a "$run_mpisol" = "yes" ]; then
    if [ -f pbs/mpisol.pbs -a "$queue_manager" == "1" ]; then
	echo -en $CYAN"submitting MPISOL++ : " $NORMAL
	qsub pbs/mpisol.pbs& 
    else
	sh run\ MPISOL++ no
    fi
fi
